﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XMK.Tadong.Biz;
using XMK.Tadong.Model;
using System.Data;
using XMK.Utils;

namespace XMK.Tadong.Web.SpaceClassView
{
    /// <summary>
    /// TreeForld 的摘要说明
    /// </summary>
    public class TreeForld : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            string currrentForldId = context.Request.Params.Get("forldId");//当前文件夹Id
            string spaceType = context.Request.Params.Get("spaceType");//空间类别10-个人空间，20-课题组空间，30-项目库空间ring
            string personID = context.Request.Params.Get("userId"); ;//LoginUserBiz.LoginUserID();
            string teamID = context.Request.Params.Get("teamId");


            DataTable dt_tree = new DataTable();
            dt_tree.Columns.Add("ObjectID", Type.GetType("System.Int32"));
            dt_tree.Columns.Add("ObjectName", Type.GetType("System.String"));
            dt_tree.Columns.Add("PObjectID", Type.GetType("System.Int32")); 
            List<TB_Tree_ObjectInfo> objInfos=null;
            if (spaceType == "30")
            {
                currrentForldId = String.IsNullOrEmpty(currrentForldId) ? "0" : currrentForldId;
                TB_Tree_ObjectBiz objBiz = new TB_Tree_ObjectBiz();
                objInfos = objBiz.GetChildById(Convert.ToInt32(currrentForldId), TreeType.XMK);
               
            }
           else if (spaceType == "10")
           {
               currrentForldId = String.IsNullOrEmpty(currrentForldId) ? "0" : currrentForldId;
               TB_Tree_ObjectBiz objBiz = new TB_Tree_ObjectBiz();

               objInfos = objBiz.QueryChildObjectById(Convert.ToInt32(currrentForldId), personID, null, "PERSON");
           }
           else if (spaceType == "20")
           {
               currrentForldId = String.IsNullOrEmpty(currrentForldId) ? "0" : currrentForldId;
               TB_Tree_ObjectBiz objBiz = new TB_Tree_ObjectBiz();
               objInfos = objBiz.QueryChildObjectById(Convert.ToInt32(currrentForldId), null, teamID, "TEAM");
           }
            context.Response.ContentType = "text/json";
            foreach (TB_Tree_ObjectInfo obj in objInfos)
            {
                DataRow newRow = dt_tree.NewRow();
                newRow["ObjectID"] = obj.ObjectID;
                newRow["ObjectName"] = obj.ObjectName;
                newRow["PObjectID"] = obj.PObjectID;
                dt_tree.Rows.Add(newRow);
            }
            string aaa = JsonHelper.ToJson(dt_tree);
            
            context.Response.Write(JsonHelper.ToJson(dt_tree));
        

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}